<?php
namespace app\common\controller;
use think\Controller;
use PHPExcel_IOFactory;
 
class PhpExcel extends Controller
{
    public function readExcel($url)
    {
        $filePath = $this->_download($url);

        // 通过IOFactory加载Excel文件
        $spreadsheet = PHPExcel_IOFactory::load($filePath);

        // 获取第一个工作表
        $worksheet = $spreadsheet->getActiveSheet();

        // 获取最大行数和最大列数
        $highestRow = $worksheet->getHighestRow();
        $highestColumn = $worksheet->getHighestColumn();

        $sheetData = $spreadsheet->getActiveSheet()->toArray(null,true,true,true);

        // 输出读取到的数据
        var_dump($sheetData);
                
    }
    // 通过curl下载网络上的文件
    private function _download($url){
        $fileName = explode('.', $url);
        $fileName = $fileName[count($fileName) - 1];

        //本地文件路径
        $local_file_path = ROOT_PATH.'public/excel/'.time().'.'.$fileName;

        //初始化CURL句柄
        $curl = curl_init();

        //设置CURL句柄
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);

        //执行CURL操作
        $file_data = curl_exec($curl);

        //关闭CURL句柄
        curl_close($curl);

        //将下载的文件保存到本地
        $fp = fopen($local_file_path, 'wb');
        fwrite($fp, $file_data);
        fclose($fp);

        return $local_file_path;
    }
}